﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.DTO;
using LuShanTea.IService;
using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class PermissionController : Controller
    {
        public IPermissionService PermissionService { get; set; }

        [CheckPermission("Permission.List")]
        // GET: Permission
        public ActionResult List()
        {
            PermissionDTO[] permissionDTOS = PermissionService.GetAll();

            return View(permissionDTOS);
        }

        [CheckPermission("Permission.Add")]
        [HttpGet]
        public ActionResult AddNew()
        {
            return View();
        }

        [CheckPermission("Permission.Add")]
        [HttpPost]
        public ActionResult AddNew(string name, string desc)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            if (PermissionService.GetByName(name) != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = name + "权限项已存在" });
            }

            PermissionService.AddNew(name, desc);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Permission.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            var permission = PermissionService.GetById(id);

            if (permission == null)
            {
                return Content("未找到ID为" + id + "的权限项");
            }

            return View(permission);
        }

        [CheckPermission("Permission.Edit")]
        [HttpPost]
        public ActionResult Edit(PermissionDTO permissionDTO)
        {
            //验证数据
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            //获取数据判断权限项是否存在
            var permission = PermissionService.GetById(permissionDTO.Id);

            if (permission == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到ID为" + permissionDTO.Id + "的权限项,请检查是否已被删除！" });
            }

            //获取名称判断修改的权限项是否已存在并且不为自身原有名称
            var dataPermission = PermissionService.GetByName(permissionDTO.Name);

            if (dataPermission != null && permission.Id != dataPermission.Id)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = permissionDTO.Name + "权限项已存在" });
            }

            PermissionService.Update(permissionDTO.Id, permissionDTO.Name, permissionDTO.Descible);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Permission.Delete")]
        [HttpPost]
        public ActionResult Delete(long permissionId)
        {
            PermissionService.MarkDeleted(permissionId);

            return Json(new AjaxResult { Status = "ok" });
        }

        [CheckPermission("Permission.Delete")]
        [HttpPost]
        public ActionResult BatchDel(long[] permissionIds)
        {
            foreach (var perId in permissionIds)
            {
                PermissionService.MarkDeleted(perId);
            }

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}